﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Web;
using LabMS.Model;

namespace Base
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 页面查看权限，或的关系
        /// </summary>
        public List<string> strPagePrivilege = new List<string>();

        /// <summary>
        /// 页面查看用户类型，或的关系
        /// </summary>
        public List<string> strPageUserType = new List<string>();

        public string UserType = "";
        public string UserName = "";
        public string UserID = "";
        public string UserCode = "";
        public string UserRealName = "";
        public List<LabMS.Model.Function> listFunction = new List<LabMS.Model.Function>();
        public List<LabMS.Model.Module> listModule = new List<LabMS.Model.Module>();
        LabMS.BLL.Module bModule = new LabMS.BLL.Module();
        public List<LabMS.Model.Plate> listPlate = new List<LabMS.Model.Plate>();
        LabMS.BLL.Plate bPlate = new LabMS.BLL.Plate();

        protected override void OnPreInit(EventArgs e)
        {
            if (PrivilegeManager.GetCurrentUserInfo())
            {
                bool bwrong = false;
                try
                {
                    UserName = System.Web.HttpContext.Current.Session["UserName"].ToString();
                    UserID = System.Web.HttpContext.Current.Session["UserID"].ToString();
                    UserCode = System.Web.HttpContext.Current.Session["UserCode"].ToString();
                    UserType = System.Web.HttpContext.Current.Session["UserType"].ToString();
                   
                    if (UserType == "mana")
                    {
                        listFunction = System.Web.HttpContext.Current.Session["UserPrivilege"] as List<LabMS.Model.Function>;
                        if (UserName == "admin")
                        {
                            listModule = bModule.GetModelList("");
                            listPlate = bPlate.GetModelList("");
                        }
                        else
                        {
                            if (System.Web.HttpContext.Current.Session["UserModule"] != null)
                            {
                                listModule = System.Web.HttpContext.Current.Session["UserModule"] as List<LabMS.Model.Module>;
                            }
                            else
                            {
                                listModule = ExtendBLL.Privilege.GetManaModuleByUserID(UserID);
                                System.Web.HttpContext.Current.Session["UserModule"] = listModule;
                            }
                            if (System.Web.HttpContext.Current.Session["UserPlate"] != null)
                            {
                                listPlate = System.Web.HttpContext.Current.Session["UserPlate"] as List<LabMS.Model.Plate>;
                            }
                            else
                            {
                                listPlate = ExtendBLL.Privilege.GetManaPlateByUserID(UserID);
                                LabMS.Model.Plate mp = new LabMS.Model.Plate();
                                bool bhas = false;
                                foreach (LabMS.Model.Plate p in listPlate)
                                {
                                    if (p.Name == "日常办公")
                                    {
                                        bhas = true;
                                        break;
                                    }
                                }
                                if (!bhas)
                                {
                                    mp.Name = "日常办公";
                                    mp.PageAddress = "Structure/DailyOffice.aspx";
                                    listPlate.Add(mp);
                                }
                                System.Web.HttpContext.Current.Session["UserPlate"] = listPlate;
                            }
                            
                        }
                    }
                    UserRealName = UserName;
                    if (System.Web.HttpContext.Current.Session["UserType"].ToString().Equals("mana"))
                    {
                        if (UserCode == "")
                        {
                            UserRealName = UserName;
                        }
                        else
                        {
                            if (System.Web.HttpContext.Current.Session["UserRealName"] != null)
                            {
                                UserRealName = System.Web.HttpContext.Current.Session["UserRealName"].ToString();
                            }
                            else
                            {
                                LabMS.BLL.Staff bstaff = new LabMS.BLL.Staff();
                                List<LabMS.Model.Staff> listStaff = new List<LabMS.Model.Staff>();
                                listStaff = bstaff.GetModelList(" Staff_Code = '" + UserCode + "'");
                                if (listStaff.Count > 0)
                                {
                                    UserRealName = listStaff[0].Staff_Name;
                                }
                                System.Web.HttpContext.Current.Session["UserRealName"] = UserRealName;
                            }
                        }
                    }
                }
                catch
                {
                    bwrong = true;
                }
                if (bwrong)
                {
                    Response.Redirect("~/login.aspx");
                }
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }
            base.OnPreInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (UserName != "admin")
            {
                if (ValUserType())
                {
                }
                else
                {
                    string strURL = "";
                    strURL = Server.UrlEncode("没有权限");
                    Response.Redirect("~/err.aspx?Message=" + strURL);
                }
                if (UserType == "mana")
                {
                    if (HavePrivilege())
                    {

                    }
                    else
                    {
                        string strURL = "";
                        strURL = Server.UrlEncode("没有权限");
                        Response.Redirect("~/err.aspx?Message=" + strURL);
                    }
                }
            }
            base.OnPreRender(e);
        }

        bool HavePrivilege()
        {
            if (strPagePrivilege.Count > 0)
            {
                foreach (string s in strPagePrivilege)
                {
                    foreach (LabMS.Model.Function f in listFunction)
                    {
                        if (f.Code == s)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 判断是否具有待定的权限
        /// </summary>
        /// <param name="privilegeCode">权限代码</param>
        /// <returns></returns>
        public bool HavePrivilege(string privilegeCode)
        {
            foreach (LabMS.Model.Function f in listFunction)
            {
                if (f.Code == privilegeCode)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断是否具有待定的模块权限
        /// </summary>
        /// <param name="privilegeCode">权限代码</param>
        /// <returns></returns>
        public bool HaveModulePrivilege(string privilegeCode)
        {
            foreach (LabMS.Model.Module m in listModule)
            {
                if (m.Code == privilegeCode)
                {
                    return true;
                }
            }
            return false;
        }

        bool ValUserType()
        {
            if (strPageUserType.Count > 0)
            {
                foreach (string s in strPageUserType)
                {
                    if (s == UserType)
                    {
                        return true;
                    }
                }
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
